package com.hbsoft.main;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;

import com.ant4j.popularize.ui.MainFrame;
import com.hbsoft.com.JAutoCompleteComboBox;
import com.hbsoft.dao.LoginDao;
import com.hbsoft.vo.UserInfoVo;

public class LoginDialog extends JFrame {
	/**
	 * 
	 */
	private JPanel mainPanel = null;

	private JAutoCompleteComboBox userBox = null;

	private JPasswordField passWorld = null;

	private JButton enter = null;

	private JButton setAgain = null;

	private JButton sysOut = null;

	private ImageIcon bgImg = new ImageIcon(this.getClass().getResource(
			"/images/enterImage.jpg"));

	private ImageIcon Out = new ImageIcon(this.getClass().getResource(
			"/images/out.jpg"));

	private ImageIcon nameLabel = new ImageIcon(this.getClass().getResource(
			"/images/1.jpg"));

	private ImageIcon passWordLabel = new ImageIcon(this.getClass()
			.getResource("/images/2.jpg"));

	private JLabel imgLabel = new JLabel(bgImg);

	private JLabel img1Lbel = new JLabel(nameLabel);

	private JLabel img2Lbel = new JLabel(passWordLabel);

	private static String userName = null;

	private JProgressBar aJProgressBar = new JProgressBar(0, 100);

	private LoginDao log = null;
	
	private HashMap<String, String> map = null;

	public LoginDialog() {
	
		initFrame();
		setSize(447, 249);
		setTitle("��¼ϵͳ");
		setUndecorated(true);
		log = new LoginDao();
		getRootPane().isFocusCycleRoot();

		mainPanel = new JPanel();
		mainPanel.setLayout(null);
		userBox = new JAutoCompleteComboBox();
		userBox.addItem("");
		userBox.setSelectedItem("");
		userBox.setOpaque(false);

		passWorld = new JPasswordField();

		enter = new JButton("��¼");
		setAgain = new JButton("����");
		sysOut = new JButton();
		sysOut.setBorderPainted(false);
		sysOut.setIcon(Out);
		img1Lbel.setIcon(nameLabel);
		img2Lbel.setIcon(passWordLabel);
		mainPanel.add(userBox);
		mainPanel.add(passWorld);
		mainPanel.add(enter);
		mainPanel.add(setAgain);
		mainPanel.add(sysOut);
		mainPanel.add(img1Lbel);
		mainPanel.add(img2Lbel);
		mainPanel.setOpaque(false);

		aJProgressBar.setStringPainted(true); // ��ʾ�ٷֱ��ַ�
		aJProgressBar.setIndeterminate(false); // ��ȷ���Ľ����
		aJProgressBar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		aJProgressBar.setVisible(false);
		mainPanel.add(aJProgressBar);

		add(mainPanel);

		userBox.setBounds(251, 106, 147, 23);
		passWorld.setBounds(251, 139, 148, 23);
		enter.setBounds(230, 180, 76, 24);
		setAgain.setBounds(321, 180, 76, 24);
		sysOut.setBounds(418, 15, 17, 15);
		img1Lbel.setBounds(140, 105, 300, 24);
		img2Lbel.setBounds(140, 138, 300, 24);
		aJProgressBar.setBounds(138, 222, 303, 24);

		initDate();
		initActionlistener();

		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void initFrame() {
		this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
		imgLabel.setBounds(0, 0, bgImg.getIconWidth(), bgImg.getIconHeight());
		((JPanel) getContentPane()).setOpaque(false); // ��JFrame���Դ���������Ϊ͸�������򱳾�ͼƬ������ʾ����
		setSize(bgImg.getIconWidth() + 8, bgImg.getIconHeight() + 25);
		Image img = this.getToolkit().getImage(
				getClass().getResource("/images/com/title.gif"));
		setIconImage(img);
	}

	public void initActionlistener() {

		sysOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		passWorld.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				enter.doClick();
			}

		});

		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Check();
				
			}
		});

		setAgain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userBox.setSelectedItem("");
				passWorld.setText(null);
			}
		});
	}

	private void Check()
	{
		if(userBox.getSelectedItem().equals(""))
		{
			JOptionPane.showMessageDialog(null,"�˺Ų���Ϊ�գ�","�������",
					JOptionPane.ERROR_MESSAGE);
		}
		else if(passWorld.getPassword().equals(""))
		{
			JOptionPane.showMessageDialog(null,"���벻��Ϊ�գ�","�������",
					JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			boolean ispsd = false;
			Set<String> set =  map.keySet();
			Iterator ite = set.iterator();
			while(ite.hasNext())
			{
				String key = (String)ite.next();
				String psd = map.get(key);
				
				char[] c  = passWorld.getPassword();
				String p = String.valueOf(c);
				
				if(psd.equals(p))
				{
					ispsd = true;
				}
			}
			if(ispsd)
			{
				MainFrame mf = MainFrame.getMainFace();
				mf.setVisible(true);
				setVisible(false);
			}
			else
			{
				JOptionPane.showMessageDialog( null,"������д����", "�������",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	public String getUserName() {
		return userBox.getText();
	}
	
	private void initDate()
	{
		
		map = new HashMap<String, String>();
		try {
			
			List<UserInfoVo> list = log.getUserInfoVoList();
			Iterator ite = list.iterator();
			while(ite.hasNext())
			{
				UserInfoVo vo = (UserInfoVo) ite.next();
				userBox.addItem(vo.getUserName());
				map.put(vo.getUserName(), vo.getPassword());
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
